package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.dto.CourseDetailData;
import cn.itsource.ymcc.dto.CourseDto;
import cn.itsource.ymcc.dto.CourseOrderDto;
import cn.itsource.ymcc.service.ICourseService;
import cn.itsource.ymcc.domain.Course;
import cn.itsource.ymcc.query.CourseQuery;
import cn.itsource.ymcc.result.JSONResult;
import cn.itsource.ymcc.result.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    //秒杀订单
    @RequestMapping(value="/infoKill/{id}/{orderNo}",method= RequestMethod.GET)
    public JSONResult infoKill(@PathVariable("id") String id,@PathVariable("orderNo") String orderNo){
        CourseOrderDto dto = courseService.infoKill(id,orderNo);
        return JSONResult.success(dto);
    }

    //订单
    @RequestMapping(value="/info/{id}",method= RequestMethod.GET)
    public JSONResult info(@PathVariable("id") String id){
        CourseOrderDto dto = courseService.info(id);
        return JSONResult.success(dto);
    }

    @RequestMapping(value="/detail/data/{id}",method= RequestMethod.GET)
    public JSONResult detailData(@PathVariable("id") Long id){
        CourseDetailData data = courseService.detailData(id);
        return JSONResult.success(data);
    }

    @PreAuthorize("hasAnyAuthority('course:online')")
    @RequestMapping(value="/onLineCourse/{id}",method= RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("id") Long id){
        courseService.onLineCourse(id);
        return JSONResult.success();
    }

    @RequestMapping(value="/offLineCourse/{id}",method= RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("id") Long id){
        courseService.offLineCourse(id);
        return JSONResult.success();
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody @Valid CourseDto dto){
        if(dto.getCourse().getId()!=null){
            courseService.updateById(dto.getCourse());
        }else{
            courseService.save(dto);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new  Page<Course>(query.getPage(),query.getRows());
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
